/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://github.com/mineadmin
 */
import type { MaProTableColumns, MaProTableExpose } from "@mineadmin/pro-table";
import type { UseDialogExpose } from "@/hooks/useDialog.ts";

import defaultAvatar from "@/assets/images/defaultAvatar.jpg";
import { ElTag } from "element-plus";
import { useMessage } from "@/hooks/useMessage.ts";
import { ResultCode } from "@/utils/ResultCode.ts";
import hasAuth from "@/utils/permission/hasAuth.ts";
import { deleteByIds } from "$/attractions/commerce/api/commerceOrder.ts";
import { page as commodityTypePage } from "$/attractions/commerce/api/commerceCommodityType.ts";
import { page as merchantPage } from "$/attractions/commerce/api/commerceMerchant.ts";

export default function getTableColumns(
  dialog: UseDialogExpose,
  formRef: any,
  t: any
): MaProTableColumns[] {
  const msg = useMessage();

  const merchantList = ref<any[]>([]);

  onMounted(async () => {
    const res2 = await merchantPage({
      page: 1,
      pageSize: 1000,
    });
    if (res2.code === 200 && res2.data && res2.data.list) {
      merchantList.value = res2.data.list;
    }
  });

  return [
    {
      type: "selection",
      showOverflowTooltip: false,
      label: () => t("crud.selection"),
    },
    { label: "订单编号", prop: "id" },
    {
      label: "商户名称",
      prop: "merchant_id",
      cellRender: ({ row }) => {
        const merchantName = merchantList.value.find(
          (merchant) => merchant.id === row.merchant_id
        );
        return merchantName?.merchant_name || `商户名称${row.merchant_id}`;
      },
    },
    { label: "客户姓名", prop: "customer_name" },
    { label: "客户电话", prop: "customer_phone" },
    { label: "客户地址", prop: "customer_address" },
    {
      label: "订单来源",
      prop: "order_source",
      cellRender: ({ row }) => {
        const orderSource = {
          1: { text: "线上下单", type: "success" },
          2: { text: "现场订单", type: "warning" },
        };
        return h(
          ElTag,
          { type: orderSource[row.order_source].type },
          () => orderSource[row.order_source].text
        );
      },
    },
    { label: "总金额", prop: "total_amount" },
    { label: "实付金额", prop: "actual_amount" },
    {
      label: "付款方式",
      prop: "payment_method",
      cellRender: ({ row }) => {
        const statusMap = {
          1: { text: "银行转账", type: "success" },
          2: { text: "微信支付", type: "warning" },
          3: { text: "支付宝支付", type: "info" },
          4: { text: "现金", type: "danger" },
          5: { text: "其他付款方式", type: "info" },
        };
        const status = statusMap[row.payment_method] || {
          text: "未知",
          type: "info",
        };
        return h(ElTag, { type: status.type }, () => status.text);
      },
    },
    {
      label: "订单状态",
      prop: "order_status",
      cellRender: ({ row }) => {
        const statusMap = {
          1: { text: "待支付", type: "success" },
          2: { text: "待发货", type: "warning" },
          3: { text: "待收货", type: "info" },
          4: { text: "已完成", type: "danger" },
          5: { text: "已取消", type: "info" },
        };
        const status = statusMap[row.order_status] || {
          text: "未知",
          type: "info",
        };
        return h(ElTag, { type: status.type }, () => status.text);
      },
    },
    { label: "支付时间", prop: "payment_time", mode: "date", showTime: true },
    { label: "发货时间", prop: "delivery_time", mode: "date", showTime: true },
    { label: "快递公司", prop: "express_company" },
    { label: "快递单号", prop: "express_id", renderProps: { multiple: false } },
    { label: "取消原因", prop: "cancel_reason" },
    { label: "取消时间", prop: "cancel_time", mode: "date", showTime: true },
    { label: "完成时间", prop: "finish_time", mode: "date", showTime: true },
    {
      type: "operation",
      label: () => t("crud.operation"),
      fixed: "right",
      operationConfigure: {
        actions: [
          // {
          //   name: "edit",
          //   icon: "material-symbols:person-edit",
          //   show: ({ row }) => hasAuth("commerce_order:commerceOrder:update"),
          //   text: () => t("crud.edit"),
          //   onClick: ({ row }) => {
          //     dialog.setTitle(t("crud.edit"));
          //     dialog.open({ formType: "edit", data: row });
          //   },
          // },
          {
            name: "deliver",
            icon: "ep:van",
            show: ({ row }) => hasAuth("commerce_order:commerceOrder:deliver") && row.order_status === 2,
            text: () => t("operation.deliver"),
            onClick: ({ row }) => {
              dialog.setTitle(t("operation.deliver"));
              dialog.open({ formType: "deliver", data: row });
            },
          },
          {
            name: "del",
            icon: "mdi:delete",
            text: () => t("crud.delete"),
            show: ({ row }) => hasAuth("commerce_order:commerceOrder:delete"),
            onClick: ({ row }, proxy: MaProTableExpose) => {
              msg.delConfirm(t("crud.delDataMessage")).then(async () => {
                const response = await deleteByIds([row.id]);
                if (response.code === ResultCode.SUCCESS) {
                  msg.success(t("crud.delSuccess"));
                  proxy.refresh();
                }
              });
            },
          },
        ],
      },
    },
  ];
}
